/*
* CheckingAccount
*
* r3
*
* GNU General Public License v3.0
*/

package com.acme.bankapp.domain.account;

import com.acme.bankapp.exceptions.*;

public class CheckingAccount extends AbstractAccount {
	
	private double balance;
	private double overdraft;
	
	public CheckingAccount(final double balance, final double overdraft) {
		
		if(balance < 0 || overdraft < 0) {
			throw new IllegalArgumentException();
		}
		
		this.balance = balance;
		this.overdraft = overdraft;
	}
	
	@Override
	public void deposit(final double amount) {
		if(amount < 0) {
			throw new IllegalArgumentException();
		}
		
		this.balance += amount;
	}

	@Override
	public void withdraw(final double amount) throws OverDraftLimitExceededException {
		if(amount < 0) {
			throw new IllegalArgumentException();
		}
		
		if(maximumAmountToWithdraw() < amount) {
			throw new OverDraftLimitExceededException(this, amount, maximumAmountToWithdraw());
		}
		
		this.balance -= amount;
		
		if(this.balance < 0) {
			this.overdraft += this.balance;
			this.balance = 0;
		}
		
		assert this.balance >= 0;
		assert this.overdraft >= 0;
	}
	
	@Override
	public double getBalance() {
		return this.balance;
	}
	
	@Override
	public double maximumAmountToWithdraw() {
		return this.balance + this.overdraft;
	}
	
}
